﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Razor;
using Think.Lenovo.Com.Models;
using Think.Lenovo.Com.DataAccess;

namespace Think.Lenovo.Com.Controllers
{
    public class RoleController : Controller
    {
        /// <summary>
        /// 返回角色管理界面
        /// </summary>
        /// <returns></returns>
        public ActionResult ViewManageRole()
        {
            #region//查出所有角色
            Roles r = new Roles();
            LinkedList<RoleModel> list = r.GetRoles();
            if (list == null)
                list = new LinkedList<RoleModel>();
            ViewData["list"] = list;
            #endregion
            return View("/Views/ViewManageRole.cshtml");
        }
        /// <summary>
        /// 添加一个角色
        /// </summary>
        /// <param name="model">角色实体</param>
        /// <returns></returns>
        public ActionResult AddRole(RoleModel model)
        {
            Dictionary<Object, Object> result = new Dictionary<Object, Object>();
            LinkedList<String> errors = new LinkedList<String>();
            if (model == null)
            {
                errors.AddLast("角色实体为null");
            }
            result.Add("errors", errors);
            Roles r = new Roles();
            long id = r.AddRole(model);
            result.Add("isSuccess", id);
            JsonResult jr = new JsonResult();
            jr.Data = result;
            return jr;
        }
        /// <summary>
        /// 编辑角色
        /// </summary>
        /// <param name="model">角色实体</param>
        /// <returns></returns>
        public JsonResult EditRole(RoleModel model)
        {
            Dictionary<Object, Object> result = new Dictionary<Object, Object>();
            LinkedList<String> errors = new LinkedList<String>();
            if (model == null)
            {
                errors.AddLast("角色实体为null");
            }
            result.Add("errors", errors);
            Roles r = new Roles();
            int affect = r.UpdateRole(model);
            result.Add("isSuccess", affect);
            JsonResult jr = new JsonResult();
            jr.Data = result;
            return jr;
        }
        /// <summary>
        /// 得到一个角色实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult GetRole(String id)
        {
            Dictionary<Object, Object> result = new Dictionary<Object, Object>();
            LinkedList<String> errors = new LinkedList<String>();
            int i = 0;
            if (!Int32.TryParse(id, out i))
            {
                errors.AddLast("角色id小于等于零");
            }
            result.Add("errors", errors);
            Roles r = new Roles();
            RoleModel model = r.GetRole(i);
            result.Add("model", model);
            JsonResult jr = new JsonResult();
            jr.Data = result;
            return jr;
        }
        /// <summary>
        /// 将一个用户角色关联N个等级;
        /// </summary>
        /// <returns></returns>
        public JsonResult RelateUserLevel()
        {
            String roleIdString = Request["roleId"];
            String userLevelIds = Request["userLevelIds"];
            LinkedList<String> errors = new LinkedList<string>();
            String isSuccess = String.Empty;
            int i;
            int roleId;
            if (!Int32.TryParse(roleIdString, out roleId))
            {
                errors.AddLast("角色id不是数字");
            }
            else if (roleId < 1)
            {
                errors.AddLast("角色id小于等于零");
            }

            List<String> lst = new List<string>();
            List<int> ids = new List<int>();
            if (userLevelIds != null)
            {
                lst = userLevelIds.Split(',').ToList();
                foreach (String s in lst)
                {
                    if (!Int32.TryParse(s, out i))
                    {
                        errors.AddLast(String.Format("等级id为{0},它不是数字.",s));
                    }
                    else if (i < 1)
                    {
                        errors.AddLast(String.Format("等级id为{0},它小于等于零.",s));
                    }
                    else
                    {
                        ids.Add(i);
                    }
                }
            }
            else {
                errors.AddLast("没有选择等级");
            }

            if (errors.Count < 1 && ids.Count > 0 && roleId > 0)
            {
                RoleUserLevelRelation rur = new RoleUserLevelRelation();

                int affect = rur.Delete(roleId);
                if (affect < 0)
                {
                    errors.AddLast("重置前,删除此角色原有关联失败!");
                }
                foreach (int item in ids)
                {
                    long id = rur.Add(roleId, item);
                    if (id < 1)
                    {
                        errors.AddLast(String.Format("角色id为{0},等级id为{1}的记录插入失败!", roleId, item));
                    }
                }
            }
            Dictionary<Object, Object> result = new Dictionary<Object, Object>();
            result.Add("errors", errors);
            if (errors.Count < 1)
            {
                result.Add("isSuccess", 1);
            }
            else
            {
                result.Add("isSuccess", -1);
            }
            JsonResult jr = new JsonResult();
            jr.Data = result;
            return jr;
        }
    }
}
